package com.lk.mall.controller.admin;

import com.lk.mall.config.annotation.TokenToAdminUser;
import com.lk.mall.model.entity.AdminUserToken;
import com.lk.mall.model.entity.Carousel;
import com.lk.mall.model.entity.IndexConfig;
import com.lk.mall.model.vo.MallCarouselVo;
import com.lk.mall.service.ICarouselService;
import com.lk.mall.util.PageQueryUtil;
import com.lk.mall.util.PageResult;
import com.lk.mall.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lemonade
 * @description
 * @updateTime 2022/9/9 11:24
 */
@Api(value = "轮播图", tags = "CarouselAPI")
@Slf4j
@RestController
@RequestMapping("/manage-api/v1")
public class CarouselAPI {

    ICarouselService iCarouselService;

    public CarouselAPI(ICarouselService iCarouselService) {
        this.iCarouselService = iCarouselService;
    }

    @RequestMapping(value = "/carousels", method = RequestMethod.GET)
    @ApiOperation("轮播图列表")
    public R<PageResult<Carousel>> list(@RequestParam(required = false) @ApiParam(value = "页码") Integer pageNumber,
                  @RequestParam(required = false) @ApiParam(value = "每页条数") Integer pageSize,
                  @TokenToAdminUser AdminUserToken adminUser) {
        log.info("adminUser:{}", adminUser.toString());
        // 校验
        if (pageNumber == null || pageNumber < 1 || pageSize == null || pageSize < 10) {
            return new R<PageResult<Carousel>>().fail("分页参数异常！");
        }

        // 返回结果
        Map<String, Object> params = new HashMap<>();
        params.put("page", pageNumber);
        params.put("limit", pageSize);
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        PageResult<Carousel>  carouselPage = iCarouselService.getCarouselPage(pageQueryUtil);

        return new R<PageResult<Carousel>>().ok(carouselPage);
    }


}
